package com.m.IO.bytes;

import java.io.*;

/**
 *  字节过滤流
 *  提高IO的效率，减少访问磁盘的次数
 *  BufferedOutputStream将FileOutputStream进行包装，可以将数据放入缓存区
 *  flush()将缓存区中的内容存入磁盘中
 *  或者是close()关闭流，会级联的调用flush()
 *
 *  BufferedInputStream和使用while(true){...} 自己创建数组进行缓冲没有什么区别
 *
 * @author Mc.Chai
 * @program: Java基础
 * @create 2021-11-21-10:42 10:42
 */
public class TestBufferedStream {
    public static void main(String[] args) throws IOException{
        FileOutputStream outputStream=new FileOutputStream("Buffer.txt");
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(outputStream);
        bufferedOutputStream.write('A');
        bufferedOutputStream.write('B');
        bufferedOutputStream.write('C');
        bufferedOutputStream.write('D');
        bufferedOutputStream.flush();
        FileInputStream fileInputStream=new FileInputStream("Buffer.txt");
        byte[] cache=new byte[4];
        while (true){
            int n=fileInputStream.read(cache);
            if (n==-1)break;
            for (int i = 0; i < n; i++) {
                System.out.println((char)cache[i]);

            }
        }
    }
}
